package ypx;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Scanner;

public class Menu {
    private static final ArrayList<Student> students = new ArrayList<>();
    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("\n----- 学生管理系统 -----");
            System.out.println("1. 添加学生");
            System.out.println("2. 删除学生");
            System.out.println("3. 修改学生");
            System.out.println("4. 查询学生");
            System.out.println("5. 退出系统");
            System.out.print("请选择(1-5): ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 清除输入缓冲区

            switch (choice) {
                case 1 -> addStudent();
                case 2 -> deleteStudent();
                case 3 -> updateStudent();
                case 4 -> findStudent();
                case 5 -> {
                    System.out.println("系统已退出");
                    System.exit(0);
                }
                default -> System.out.println("无效输入，请重新选择");
            }
        }
    }

    // 添加学生
    private static void addStudent() {
        System.out.println("\n--- 添加学生 ---");
        System.out.print("学号: ");
        String id = scanner.nextLine();

        if (findById(id) != null) {
            System.out.println("该学号已存在！");
            return;
        }

        System.out.print("姓名: ");
        String name = scanner.nextLine();
        System.out.print("年龄: ");
        int age = scanner.nextInt();
        System.out.print("性别: ");
        String gender = scanner.next();
        System.out.print("成绩: ");
        double score = scanner.nextDouble();
        scanner.nextLine(); // 清除缓冲区

        students.add(new Student(id, name, age, gender, score));
        System.out.println("添加成功！");
    }

    // 删除学生
    private static void deleteStudent() {
        System.out.println("\n--- 删除学生 ---");
        System.out.print("输入要删除的学生学号: ");
        String id = scanner.nextLine();//储存学号

        Student s = findById(id);
        if (s == null) {
            System.out.println("未找到该学生");
            return;
        }

        students.remove(s);
        System.out.println("删除成功！");
    }

    // 修改学生
    private static void updateStudent() {
        System.out.println("\n--- 修改学生 ---");
        System.out.print("输入要修改的学生学号: ");
        String id = scanner.nextLine();

        Student s = findById(id);
        if (s == null) {
            System.out.println("未找到该学生");
            return;
        }

        System.out.println("当前信息: " + s);
        System.out.print("新姓名: ");
        s.setName(scanner.nextLine());
        System.out.print("新年龄: ");
        s.setAge(scanner.nextInt());
        System.out.print("新性别: ");
        s.setGender(scanner.next());
        System.out.print("新成绩: ");
        s.setAddress(String.valueOf(scanner.nextDouble()));
        scanner.nextLine(); // 清除缓冲区

        System.out.println("修改成功！");
    }

    // 查询学生
    private static void findStudent() {
        System.out.println("\n--- 查询学生 ---");
        System.out.print("输入学号(输入all查看全部): ");
        String input = scanner.nextLine();

        if (input.equalsIgnoreCase("all")) {
            if (students.isEmpty()) {
                System.out.println("没有学生记录");
                return;
            }
            System.out.println("学号\t姓名\t年龄\t性别\t成绩");
            for (Student s : students) {
                System.out.println(s.getId() + "\t" + s.getName() + "\t" +
                        s.getAge() + "\t" + s.getGender() + "\t" + s.getAddress());
            }
        } else {
            Student s = findById(input);
            if (s == null) {
                System.out.println("未找到该学生");
            } else {
                System.out.println("查询结果: " + s);
            }
        }
    }

    // 根据学号查找学生
    private static Student findById(String id) {
        for (Student s : students) {
            if (Objects.equals(s.getId(), id)) {
                return s;
            }
        }
        return null;
    }
}



